class User {
  #isTourist = true;
  #username;
  #entrusts;
  #positions;
  #usdAssert;
  #btcAssert;
  #usdTradingAssert;
  #btcTradingAssert;
  constructor({
    username,
    isTourist,
    entrusts = [],
    positions = [],
    usdAssert = 0.0,
    btcAssert = 0.0,
    usdTradingAssert = 0.0,
    btcTradingAssert = 0.0,
  }) {
    this.#positions = positions;
    this.#username = username;
    this.#isTourist = isTourist;
    this.#usdAssert = usdAssert;
    this.#btcAssert = btcAssert;
    this.#usdTradingAssert = usdTradingAssert;
    this.#btcTradingAssert = btcTradingAssert;
    this.#entrusts = entrusts;
  }

  get positions() {
    return this.#positions;
  }

  get isTourist() {
    return this.#isTourist;
  }
  get username() {
    return this.#username;
  }
  get entrusts() {
    return this.#entrusts;
  }
  get usdAssert() {
    return this.#usdAssert;
  }
  get btcAssert() {
    return this.#btcAssert;
  }
  get usdTradingAssert() {
    return this.#usdTradingAssert;
  }
  get btcTradingAssert() {
    return this.#btcTradingAssert;
  }

  completeEntrust(id, finalPrice) {
    for (const k in this.#entrusts) {
      if (this.#entrusts[k].id === id) {
        this.#entrusts[k].complete(finalPrice);
        return;
      }
    }
  }

  cancelEntrust(id) {
    let t = this.#entrusts[id];
    if (t != null) {
      t.cancel();
    }
  }

  addEntrust(entrust) {
    this.#entrusts.push(entrust);
  }

  updatePosition(position) {
    for (const k in this.#positions) {
      if (this.#positions[k].id === position.id) {
        this.#positions[k] = position;
      }
    }
  }

  removePosition(id) {
    for (const k in this.#positions) {
      if (this.#positions[k].id === id) {
        this.#positions.splice(k, 1);
        return;
      }
    }
  }

  addPosition(position) {
    for (const k in this.#positions) {
      if (this.#positions[k].id === position.id) {
        throw new Error("重复的开仓");
      }
    }
    this.#positions.push(position);
  }

  getPosition(isShort) {
    for (const k in this.#positions) {
      if (this.#positions[k].isShort === isShort) {
        return this.#positions[k];
      }
    }
    return null;
  }

  addUsdTradingAssert(num) {
    this.#usdTradingAssert += num;
    if (this.#usdTradingAssert < 0) {
      this.#usdTradingAssert = 0.0;
    }
  }

  subBtcTradingAssert(num) {
    this.#btcTradingAssert -= num;
    if (this.#btcTradingAssert < 0) {
      this.#btcTradingAssert = 0.0;
    }
    this.#btcTradingAssert = this.#btcTradingAssert;
  }

  subUsdTradingAssert(num) {
    this.#usdTradingAssert -= num;
    if (this.#usdTradingAssert < 0) {
      this.#usdTradingAssert = 0.0;
    }
    this.#usdTradingAssert = this.#usdTradingAssert;
  }
}

export default User;
